package com.cloud.util;

import org.springframework.beans.BeanUtils;

import java.util.Collection;

/**
 * @program: dentist
 * @description: ${description}
 * @author: LiuChao
 * @create: 2018-04-13 12:59
 **/
public class BeanUtil {

    /**
     * @Description: 复制List元素的属性
     * @Param: [sourceColl, targetColl, targetClass]
     * @return: void
     * @Author: LiuChao
     * @Date: 2018/4/13 13:05
     */
    public static void copyCollectionProperties(final Collection sourceColl, final Collection targetColl,
                                                final Class targetClass) {
        try {
            if (sourceColl != null && targetColl != null) {
                for (final Object source : sourceColl) {
                    if (source != null) {
                        final Object target = targetClass.newInstance();
                        BeanUtils.copyProperties(source, target);
                        targetColl.add(target);
                    }
                }
            }
        } catch (final Throwable ex) {
            throw new RuntimeException("Could not copy properties from source to target", ex);
        }
    }

    /**
     * @Description: 复制POJO元素的属性
     * @Param: [source, target]
     * @return: void
     * @Author: LiuChao
     * @Date: 2018/4/13 13:35
     */
    public static void copyProperties(final Object source, final Object target) {
        if (source != null && target != null) {
            BeanUtils.copyProperties(source, target);
        }
    }

}
